package sshFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

/**
 * 检测文件
 * @author Administrator
 *
 */
public class checkFileAndUpload {
	
	//检测特定目录下是否有新文件
	public List<String> getFiles(String filePath) {
		List<String> files = new ArrayList<String>();
        File file = new File(filePath);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
            	System.out.println("当前文件:"+tempList[i].toString());
                files.add(tempList[i].toString());
            }
            if (tempList[i].isDirectory()) {
               continue; //这里就不递归了，
            }
        }
        return files;
	}
	
	
	//解析txt文件，并根据时间拆分为多个文件
	public void analysisAndSplitFile(String filePath,String saveFilePath) {				
		String result = "";		
		try{
			
		    BufferedReader br = new BufferedReader(new FileReader(filePath));//构造一个BufferedReader类来读取文件
		    String s = null;
		    
		    Map<String,Object> map = new HashMap<String,Object>();
		    int n = 0;
		    while((s = br.readLine())!=null){//使用readLine方法，一次读一行
		    	
		    	if(n==0){
		    		n=1;
		    		s = s.substring(1,s.length());
		    	}
		    	System.out.println(s);
		    	String timeStr = s.split("LST")[0].trim();
		    	ArrayList arr = (ArrayList) map.get(timeStr);
		    	if(arr==null){
		    		arr = new ArrayList();
		    	}
		    	arr.add("LST"+s.split("LST")[1]);
		    	map.put(timeStr, arr);
		    	
		     } 
	         br.close();
	        
	        
	         Date tempDate=new Date();
	         Calendar c = Calendar.getInstance();
	         c.setTime(tempDate);
	         
	         for(String key : map.keySet()){
	        	    System.out.println(key);
	        	    
	        	    ArrayList arr = (ArrayList)map.get(key);
	        	    String str = "";
	        	    for (int i = 0; i < arr.size(); i++) {
	        	    	str += (String)arr.get(i)+"\r\n";
					}
	        	    String temp=key.trim();
		        	
		        	temp=temp.replace("月", "");
		        	temp=temp.replace("日", "");
		        	temp=temp.replace(":", "");
		        	temp=temp.replace(" ", "");
		        	temp=c.get(Calendar.YEAR)+temp;		 
		        	System.out.println(saveFilePath+File.separator+temp+".txt");
		        	
		        	File file = new File(saveFilePath+File.separator+temp+".txt");
		        	FileUtils.writeStringToFile(file, str,"GBK");
	        	    
	         }
	      }		
	   catch(Exception e){
	         e.printStackTrace();
	      }
		
	}
	//删除文件
	public void DeleFile(String filePath) {
		File file=new File(filePath);
		if (file.isFile()) {
			file.delete();
		}
	}
	
	//写入日志文件
	public void writeRecord(String content,String filePath) {
		File writefile;
		try {
			 writefile = new File(filePath);
			// 如果文本文件不存在则创建它
			 if (!writefile.exists()) {
	                writefile.createNewFile();
	                writefile = new File(filePath); // 重新实例化
	            }
			 FileOutputStream fw = new FileOutputStream(writefile,true);
			  Writer out = new OutputStreamWriter(fw, "utf-8");
			  out.write(content);
			  
			  String newline = System.getProperty("line.separator");
			  out.write(newline);  
			  
	          out.close();
	          fw.flush();
	          fw.close();
			 
			 
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
